<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>翻译单元局部实体 (C++20 起)</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/tu_local","wgTitle":"cpp/language/tu local","wgCurRevisionId":70325,"wgArticleId":13394,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/tu_local","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_tu_local skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">翻译单元局部实体 <span class="t-mark-rev t-since-cxx20">(C++20 起)</span></h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../container.html" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">翻译单元局部</strong> <span class="t-mark-rev t-since-cxx20">(C++20)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html" title="cpp/language/namespace"> 命名空间定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 链接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>翻译单元局部（ TU 局部）实体是为阻止假定为局部（不用于任何其他翻译单元）的实体暴露且用于其他翻译单元而引入的。
</p><p>来自<a rel="nofollow" class="external text" href="https://vector-of-bool.github.io/2019/03/31/modules-2.html">了解 C++ 模块：部分 2</a> 的例子描绘了不约束暴露的问题：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// 无 TU 局部约束的模块单元</span>
export module Foo<span class="sy4">;</span>
 
import <span class="sy1">&lt;</span>iostream<span class="sy1">&gt;</span><span class="sy4">;</span>
 
<span class="kw1">namespace</span> <span class="br0">{</span>
   <span class="kw1">class</span> LolWatchThis <span class="br0">{</span>        <span class="co1">// 内部链接，不能导出</span>
       <span class="kw4">static</span> <span class="kw4">void</span> say_hello<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
           <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"Hello, everyone!<span class="es1">\n</span>"</span><span class="sy4">;</span>
       <span class="br0">}</span>
   <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span>
 
export LolWatchThis lolwut<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="co1">// LolWatchThis 被暴露为返回类型</span>
    <span class="kw1">return</span> LolWatchThis<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// main.cpp</span>
import Foo<span class="sy4">;</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">auto</span> evil <span class="sy1">=</span> lolwut<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>        <span class="co1">// 'evil' 拥有 'LolWatchThis' 类型</span>
    decltype<span class="br0">(</span>evil<span class="br0">)</span><span class="sy4">::</span><span class="me2">say_hello</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 'LolWatchThis' 的定义不再为内部</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id="TU_.E5.B1.80.E9.83.A8.E5.AE.9E.E4.BD.93">TU 局部实体</span></h3>
<p>一个实体为 <i>TU 局部</i>，若它为
</p>
<ol><li> 类型、函数、变量或模板，且
<ol><li> 拥有带<a href="storage_duration.html#.E5.86.85.E9.83.A8.E8.BF.9E.E6.8E.A5" title="cpp/language/storage duration">内部链接</a>的名字，或
</li><li> 无拥有内部链接的名字，且在 TU 局部实体之内声明或由 <a href="lambda.html" title="cpp/language/lambda">lambda 表达式</a>引入、
</li></ol>
</li><li> 定义在<a href="class.html" title="cpp/language/class">类说明符</a>、函数体或初始化器外，或由用于声明仅 TU 局部实体的定义类型说明符（类型说明符、类说明符或枚举说明符）引入、
</li><li> TU 局部模板的特化、
</li><li> 有任何 TU 局部模板实参的模板特化，或
</li><li> 其声明为暴露（定义后述）的（可能实例化的）模板特化。
</li></ol>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// 拥有内部链接的 TU 局部实体</span>
<span class="kw1">namespace</span> <span class="br0">{</span> <span class="co1">// 声明于无名命名空间的所有名字均拥有内部链接</span>
    <span class="kw4">int</span> tul_var <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>                          <span class="co1">// TU 局部变量</span>
    <span class="kw4">int</span> tul_func<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span>              <span class="co1">// TU 局部函数</span>
    <span class="kw1">struct</span> tul_type <span class="br0">{</span> <span class="kw4">int</span> mem<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>             <span class="co1">// TU 局部（类）类型</span>
<span class="br0">}</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span>
<span class="kw4">static</span> <span class="kw4">int</span> tul_func_temp<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span>      <span class="co1">// TU 局部模板</span>
 
<span class="co1">// TU 局部模板特化</span>
<span class="kw1">template</span><span class="sy1">&lt;&gt;</span>
<span class="kw4">static</span> <span class="kw4">int</span> tul_func_temp<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">3</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// TU 局部特化</span>
 
<span class="co1">// 有 TU 局部模板实参的模板特化</span>
<span class="kw1">template</span> <span class="sy1">&lt;&gt;</span> <span class="kw1">struct</span> <a href="../utility/hash.html"><span class="kw1124">std::<span class="me2">hash</span></span></a><span class="sy1">&lt;</span>tul_type<span class="sy1">&gt;</span> <span class="br0">{</span>      <span class="co1">// TU 局部特化</span>
    <a href="../types/size_t.html"><span class="kw100">std::<span class="me2">size_t</span></span></a> operator<span class="br0">(</span><span class="br0">)</span><span class="br0">(</span><span class="kw4">const</span> tul_type<span class="sy3">&amp;</span> t<span class="br0">)</span> <span class="kw4">const</span> <span class="br0">{</span> <span class="kw1">return</span> 4u<span class="sy4">;</span> <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：缺少规则 #1.2、 #2 及 #5 的示例 </td></tr></table>
<p>值或对象为 <i>TU 局部</i>，若它
</p>
<ol><li> 是 TU 局部的函数或与 TU 局部变量关联的对象，或是指向它的指针，或者
</li><li> 是类或数组类型对象，而其任一<a href="object.html#.E5.AD.90.E5.AF.B9.E8.B1.A1" title="cpp/language/object">子对象</a>，或其引用类型非静态数据成员所引用的对象或函数，为 TU 局部并且<a href="constant_expression.html#.E5.8F.AF.E7.94.A8.E4.BA.8E.E5.B8.B8.E9.87.8F.E8.A1.A8.E8.BE.BE.E5.BC.8F" title="cpp/language/constant expression">可用于常量表达式</a>。
</li></ol>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">static</span> <span class="kw4">int</span> tul_var <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>             <span class="co1">// TU 局部变量</span>
<span class="kw4">static</span> <span class="kw4">int</span> tul_func<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// TU 局部函数</span>
 
<span class="kw4">int</span><span class="sy2">*</span> tul_var_ptr <span class="sy1">=</span> <span class="sy3">&amp;</span>tul_var<span class="sy4">;</span>        <span class="co1">// TU 局部：指向 TU 局部变量的指针</span>
<span class="kw4">int</span> <span class="br0">(</span><span class="sy2">*</span> tul_func_ptr<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span> <span class="sy1">=</span> <span class="sy3">&amp;</span>tul_func<span class="sy4">;</span> <span class="co1">// TU 局部：指向 TU 局部函数的指针</span>
 
<span class="kw4">constexpr</span> <span class="kw4">static</span> <span class="kw4">int</span> tul_const <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// TU 局部变量可用于常量表达式</span>
<span class="kw4">int</span> tul_arr<span class="br0">[</span><span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span> tul_const <span class="br0">}</span><span class="sy4">;</span>      <span class="co1">// TU 局部： constexpr TU 局部对象的数组</span>
<span class="kw1">struct</span> tul_class <span class="br0">{</span> <span class="kw4">int</span> mem<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
tul_class tul_obj<span class="br0">{</span>tul_const<span class="br0">}</span><span class="sy4">;</span>       <span class="co1">// TU 局部：拥有成员 constexpr TU 局部对象</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.9A.B4.E9.9C.B2">暴露</span></h3>
<p>声明 D <i>指名</i>实体 E ，若
</p>
<ol><li> D 含有闭包类型为 E 的 lambda 表达式、
</li><li> E 不是函数或函数模板且 D 含有指代 E 的标识表达式、类型说明符、嵌套类型说明符、模板名或概念名，或
</li><li> E 是函数或函数模板且 D 含有指名 E 的表达式或指代含有 E 的重载集的标识表达式。
</li></ol>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// lambda 指名</span>
<span class="kw4">auto</span> x <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span> <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 指名 decltype(x)</span>
 
<span class="co1">// 非函数（模板）指名</span>
<span class="kw4">int</span> y1 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>                      <span class="co1">// 指名 y1 （标识表达式）</span>
<span class="kw1">struct</span> y2 <span class="br0">{</span> <span class="kw4">int</span> mem<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
y2 y2_obj<span class="br0">{</span><span class="nu0">1</span><span class="br0">}</span><span class="sy4">;</span>                    <span class="co1">// 指名 y2 （类型说明符）</span>
<span class="kw1">struct</span> y3 <span class="br0">{</span> <span class="kw4">int</span> mem_func<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> y3<span class="sy4">::</span><span class="me2">mem_func</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// 指名 y3 （嵌套类型说明符）</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> <span class="kw4">int</span> y4 <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
<span class="kw4">int</span> var <span class="sy1">=</span> y4<span class="sy1">&lt;</span>y2<span class="sy1">&gt;</span><span class="sy4">;</span>                <span class="co1">// 指名 y4 （模板名）</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> concept y5 <span class="sy1">=</span> <span class="kw2">true</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">typename</span> T<span class="sy1">&gt;</span> <span class="kw4">void</span> func<span class="br0">(</span>T<span class="sy3">&amp;&amp;</span><span class="br0">)</span> requires y5<span class="sy1">&lt;</span>T<span class="sy1">&gt;</span><span class="sy4">;</span> <span class="co1">// 指名 y5 （概念名）</span>
 
<span class="co1">// 函数（模板）指名</span>
<span class="kw4">int</span> z1<span class="br0">(</span><span class="kw4">int</span> arg<span class="br0">)</span>      <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"no overload"</span><span class="sy4">;</span> <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">int</span> z2<span class="br0">(</span><span class="kw4">int</span> arg<span class="br0">)</span>      <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"overload 1"</span><span class="sy4">;</span>  <span class="kw1">return</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="br0">}</span>
<span class="kw4">int</span> z2<span class="br0">(</span><span class="kw4">double</span> arg<span class="br0">)</span>   <span class="br0">{</span> <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> <span class="st0">"overload 2"</span><span class="sy4">;</span>  <span class="kw1">return</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="br0">}</span>
 
<span class="kw4">int</span> val1 <span class="sy1">=</span> z1<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 指名 z1</span>
<span class="kw4">int</span> val2 <span class="sy1">=</span> z2<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 指名 z2 （int z2(int)）</span></pre></div></div>
<p>一个声明是<i>暴露</i>，若它要么指名一个 TU 局部实体，不过忽略
</p>
<ol><li> 非 inline 函数或函数模板的函数体（但非用<a href="auto.html" title="cpp/language/auto">占位符类型</a>声明返回类型的函数的（可能实例化的）定义的推导返回类型），
</li><li> 变量或变量模板的初始化器（但非变量类型），
</li><li> 类定义中的友元声明，及
</li><li> 任何到拥有内部链接或无链接的、以非 <a href="definition.html#ODR_.E4.BD.BF.E7.94.A8" title="cpp/language/definition">odr 使用</a>的常量表达式初始化的非 volatile 的 const 对象或引用的引用，
</li></ol>
<p>这些情况，要么定义以 TU 局部值初始化的 constexpr 变量。
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：缺少暴露的示例 </td></tr></table>
<h3><span class="mw-headline" id="TU_.E5.B1.80.E9.83.A8.E7.BA.A6.E6.9D.9F">TU 局部约束</span></h3>
<p>若<a href="modules.html" title="cpp/language/modules">模块接口单元</a>（在可能有的私有模块片段外）或模块划分中的非 TU 局部实体的（可能实例化的）<a href="declarations.html" title="cpp/language/declarations">声明</a>或<a href="class_template_argument_deduction.html#.E7.B1.BB.E6.A8.A1.E6.9D.BF.E7.9A.84.E6.8E.A8.E5.AF.BC" title="cpp/language/class template argument deduction">推导指引</a>为暴露，则程序非良构。这种声明在在任何其他语境中被弃用。
</p><p>若出现于一个翻译单元中的声明指名在另一非头文件单元的翻译单元中声明的 TU 局部实体，则程序非良构。为模板特化实例化的声明出现在特化的实例化点。
</p>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：缺少约束的示例 </td></tr></table>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<p>翻译单元 #1：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">export module A<span class="sy4">;</span>
<span class="kw4">static</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>
<span class="kw1">inline</span> <span class="kw4">void</span> it<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span>         <span class="co1">// 错误：为 f 的暴露</span>
<span class="kw4">static</span> <span class="kw1">inline</span> <span class="kw4">void</span> its<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// OK</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> its<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// OK</span>
<span class="kw1">template</span> <span class="kw4">void</span> g<span class="sy1">&lt;</span><span class="nu0">0</span><span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 
decltype<span class="br0">(</span>f<span class="br0">)</span> <span class="sy2">*</span>fp<span class="sy4">;</span>                             <span class="co1">// 错误： f （尽管不是其类型）为 TU 局部</span>
<span class="kw4">auto</span> <span class="sy3">&amp;</span>fr <span class="sy1">=</span> f<span class="sy4">;</span>                                <span class="co1">// OK</span>
<span class="kw4">constexpr</span> <span class="kw4">auto</span> <span class="sy3">&amp;</span>fr2 <span class="sy1">=</span> fr<span class="sy4">;</span>                    <span class="co1">// 错误：为 f 的暴露</span>
<span class="kw4">constexpr</span> <span class="kw4">static</span> <span class="kw4">auto</span> fp2 <span class="sy1">=</span> fr<span class="sy4">;</span>              <span class="co1">// OK</span>
<span class="kw1">struct</span> S <span class="br0">{</span> <span class="kw4">void</span> <span class="br0">(</span><span class="sy3">&amp;</span>ref<span class="br0">)</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span> s<span class="br0">{</span>f<span class="br0">}</span><span class="sy4">;</span>            <span class="co1">// OK： value 为 TU 局部</span>
<span class="kw4">constexpr</span> <span class="kw4">extern</span> <span class="kw1">struct</span> W <span class="br0">{</span> S <span class="sy3">&amp;</span>s<span class="sy4">;</span> <span class="br0">}</span> wrap<span class="br0">{</span>s<span class="br0">}</span><span class="sy4">;</span> <span class="co1">// OK： value 非 TU 局部 TU-local</span>
 
<span class="kw4">static</span> <span class="kw4">auto</span> x <span class="sy1">=</span> <span class="br0">[</span><span class="br0">]</span><span class="br0">{</span>f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span><span class="br0">}</span><span class="sy4">;</span> <span class="co1">// OK</span>
<span class="kw4">auto</span> x2 <span class="sy1">=</span> x<span class="sy4">;</span>              <span class="co1">// 错误：闭包类型为 TU 局部</span>
<span class="kw4">int</span> y <span class="sy1">=</span> <span class="br0">(</span><span class="br0">[</span><span class="br0">]</span><span class="br0">{</span>f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span><span class="br0">}</span><span class="br0">(</span><span class="br0">)</span>,<span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>   <span class="co1">// 错误：闭包类型非 TU 局部</span>
<span class="kw4">int</span> y2 <span class="sy1">=</span> <span class="br0">(</span>x,<span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>           <span class="co1">// OK</span>
 
<span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="kw1">struct</span> A <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
    <span class="kw4">void</span> adl<span class="br0">(</span>A<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">static</span> <span class="kw4">void</span> adl<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">void</span> adl<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
 
<span class="kw1">inline</span> <span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">auto</span> x<span class="br0">)</span> <span class="br0">{</span> adl<span class="br0">(</span>x<span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// OK ，但特化可能为暴露</span></pre></div></div>
<p>翻译单元 #2：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1">module A<span class="sy4">;</span>
<span class="kw4">void</span> other<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    g<span class="sy1">&lt;</span><span class="nu0">0</span><span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>                  <span class="co1">// OK：显式实例化特化</span>
    g<span class="sy1">&lt;</span><span class="nu0">1</span><span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>                  <span class="co1">// 错误：实例化使用其 TU 局部实体</span>
    h<span class="br0">(</span>N<span class="sy4">::</span><span class="me2">A</span><span class="br0">{</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span>               <span class="co1">// 错误：重载集含 TU 局部的 N::adl(int)</span>
    h<span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy4">;</span>                    <span class="co1">// OK：调用 adl(double)</span>
    adl<span class="br0">(</span>N<span class="sy4">::</span><span class="me2">A</span><span class="br0">{</span><span class="br0">}</span><span class="br0">)</span><span class="sy4">;</span>             <span class="co1">// OK：找不到 N::adl(int) ，调用 N::adl(N::A)</span>
    fr<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>                    <span class="co1">// OK：调用 f</span>
    <span class="kw4">constexpr</span> <span class="kw4">auto</span> ptr <span class="sy1">=</span> fr<span class="sy4">;</span> <span class="co1">// 错误：此处 fr 不可用于常量表达式</span>
<span class="br0">}</span></pre></div></div>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：示例太复杂，需要更好的安排 </td></tr></table>

<!-- 
NewPP limit report
Preprocessor visited node count: 2747/1000000
Preprocessor generated node count: 7086/1000000
Post‐expand include size: 70208/2097152 bytes
Template argument size: 13532/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:13394-0!*!0!!zh!*!zh!* and timestamp 20211022194828 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/tu_local&amp;oldid=70325">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/tu_local&amp;oldid=70325</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/tu_local">Online version</a></li><li>Offline version retrieved 2022-01-01 00:37.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2021年5月31日 (星期一) 02:38。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过323次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 1.149 secs. -->
	</body>
<!-- Cached 20211022194828 -->
</html>